package com.wisdytech.linkdcs.tool.action;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.handler.ResourceMatchingService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.model.EquipModel;
import com.wisdytech.linkdcs.equipment.service.IEquipmentModelService;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.tool.model.ToolConfig;
import com.wisdytech.linkdcs.tool.service.IToolConfigService;
import com.wisdytech.linkdcs.tool.vo.ToolConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanghailin
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/tool/config")
public class ToolConfigAction {

    @Resource
    private IToolConfigService toolConfigService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    @Resource
    private IEquipmentModelService equipmentModelService;

    /**
     *
     * <p>Description: 设备信息</p>
     * @param model model
     * @return String
     */
    @RequestMapping("/index")
    public String toolInfoIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
        return "tool/toolConfigure";
    }

    /**
     * <p>Description:查询设备类别编号信息列表</p>
     * @return 设备类别编号信息列表集合
     */
    @RequestMapping(value = "/getEqModelList/get.mvc")
    @ResponseBody
    public StandardResult getEqModelList(String modelCode){
        List<EquipModel> list = equipmentModelService.listAllModel(modelCode);
        return StandardResult.getSuccessResult(list);
    }

    /**
     * 分页查询接口
     * @param modelCode 必须穿 设备型号Id
     * @param filter 分页参数
     * @return 分页结果
     */
    @RequestMapping(value = "/getToolConfigByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<ToolConfigVO> getToolConfigByFilter(String modelCode, Filter filter) {
        PageInfo<ToolConfigVO> page = this.toolConfigService.getToolConfigByFilter(modelCode,filter);
        return new StandardTableResult<>(page.getTotal(),page.getList());
    }


    /**
     *
     * @param modelCode 新增工装配置时根据modelCode查询设备型号、工序等信息
     * @return StandardResult
     */
    @RequestMapping(value = "/getModelInfo/get.mvc")
    @ResponseBody
    public StandardResult getToolConfigInfo(String modelCode) {
        ToolConfigVO vo = this.toolConfigService.getToolConfigInfo(modelCode);
        return StandardResult.getSuccessResult(vo);
    }

    /**
     * 分页查询接口 select2 使用
     * @param modelCode 必须传 设备型号Id
     * @param toolCode 可以不传 工装型号Id
     * @param filter 分页参数
     * @return 分页结果
     */
    @RequestMapping(value = "/getToolListByFilter/get.mvc")
    @ResponseBody
    public StandardTableResult<ToolConfigVO> getToolListByFilter(String search,String modelCode, String toolCode,Filter filter) {
        PageInfo<ToolConfigVO> page = this.toolConfigService.getToolListByFilter(search,modelCode,toolCode,filter);
        return new StandardTableResult<>(page.getTotal(),page.getList());
    }

    /**
     * 保存/更新工装配置信息
     * @param toolConfig 实体类
     * @return StandardResult
     */
    @RequestMapping(value = "/saveOrUpdateToolConfigInfo/save.mvc")
    @ResponseBody
    public StandardResult saveOrUpdateToolConfigInfo(ToolConfig toolConfig) {
        SysUser user = BaseUtil.getCurrentUser();
        this.toolConfigService.saveOrUpdateToolConfig(toolConfig,user);
        return StandardResult.getSuccessResult();
    }

    /**
     *
     * @param pkIds 主键
     * @return StandardResult
     */
    @RequestMapping(value = "/deleteToolConfigById/del.mvc")
    @ResponseBody
    public StandardResult deleteToolConfigById(String pkIds) {
        SysUser operator = BaseUtil.getCurrentUser();
        this.toolConfigService.deleteToolConfig(pkIds,operator);
        return StandardResult.getSuccessResult();
    }

    /**
     *
     * @param pkId 主键
     * @return StandardResult
     */
    @RequestMapping(value = "/getToolConfigInfoById/get.mvc")
    @ResponseBody
    public StandardResult getToolConfigInfoById(String pkId) {
        ToolConfigVO t = this.toolConfigService.getToolConfigInfoById(pkId);
        return StandardResult.getSuccessResult(t);
    }
    
    
    /**
     * 分页查询接口 select2 使用 ,通过设备编码查询其下关联的工装
     * @param equipCode 必须传 设备型号Id
     * @param filter 分页参数
     * @return 分页结果
     */
    @RequestMapping(value = "/getEquipToolListByFilter/get.mvc")
    @ResponseBody
    public StandardResult getEquipToolListByFilter(String search,String equipCode,Filter filter) {
        PageInfo<ToolConfigVO> page = this.toolConfigService.getEquipToolListByFilter(search,equipCode,filter);
        return StandardResult.getSuccessResult(page.getList());
    }

    /**
     * <p>Description: 保存设备对象</p>
     * @param file 上传的文件对象
     * @return 处理结果对象
     */
    @RequestMapping(value = "/batchImportToolConfig/save.mvc")
    @ResponseBody
    public StandardResult batchImportToolConfig(@RequestParam(value = "file", required = false) MultipartFile file) {
        if (file==null) {
            return StandardResult.getFailResult("文件上传失败");
        }
        SysUser operator = BaseUtil.getCurrentUser();
        this.toolConfigService.batchImportToolConfig(file,operator);
        return StandardResult.getSuccessResult("数据导入成功",null);
    }

    /**
     *
     * <p>Description: 下载文件</p>
     */
    @RequestMapping(value = "/downloadImportTemplate/get.mvc")
    //@ResponseBody
    public ResponseEntity<FileSystemResource> download() throws UnsupportedEncodingException {
        //String filename = URLEncoder.encode("工装保养项导入模板.xlsx", StandardCharsets.UTF_8.displayName());
        String filename = new String("工装配置导入模板.xlsx".getBytes("gb2312"),"ISO-8859-1");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=" + filename);
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:static/excelTemplates/工装配置导入模板.xlsx");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new FileSystemResource(file));
    }


}
